package com.example.demo.controller;

import com.example.demo.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            //使用内置交换机发送消息
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, "hello spring amqp: work....");
        }
        return "发送成功";
    }

    @RequestMapping("/fanout")
    public String fanoutProduct() {
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "hello spring boot: fanout....");
        return "发送成功";
    }
    @RequestMapping("/direct")
    public String directProduct(String routingKey){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey, "hello spring boot: direct " + routingKey);
        return "发送成功";
    }
    @RequestMapping("/topic")
    public String topicProduct(String routingKey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello spring boot: topic " + routingKey);
        return "发送成功";
    }
}
